package com.luo.chengrui.module.sa.dal.mysql.org;

import java.util.*;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import com.luo.chengrui.module.sa.dal.dataobject.org.OrgDO;
import com.luo.chengrui.module.sa.dal.dataobject.org.OrgPersonDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import com.luo.chengrui.module.sa.controller.admin.org.vo.*;
import org.apache.ibatis.annotations.Param;

/**
 * 组织机构管理 Mapper
 *
 * @author 诚睿
 */
@Mapper
public interface OrgMapper extends BaseMapperX<OrgDO> {

    default PageResult<OrgDO> selectPage(OrgPageReqVO reqVO) {
        LambdaQueryWrapperX<OrgDO> wrapperX = new LambdaQueryWrapperX<OrgDO>()
                .eqIfPresent(OrgDO::getTypeId, reqVO.getTypeId())
                .eqIfPresent(OrgDO::getCode, reqVO.getCode())
                .likeIfPresent(OrgDO::getName, reqVO.getName())
                .likeIfPresent(OrgDO::getLongName, reqVO.getLongName())
                .eqIfPresent(OrgDO::getParentId, reqVO.getParentId())
                .likeIfPresent(OrgDO::getFullId, reqVO.getFullId())
                .likeIfPresent(OrgDO::getFullCode, reqVO.getFullCode())
                .likeIfPresent(OrgDO::getFullName, reqVO.getFullName())
                .eqIfPresent(OrgDO::getOrgKindId, reqVO.getOrgKindId())
                .eqIfPresent(OrgDO::getDepth, reqVO.getDepth())
                .eqIfPresent(OrgDO::getPersonId, reqVO.getPersonId())
                .eqIfPresent(OrgDO::getNodeKindId, reqVO.getNodeKindId())
                .likeIfPresent(OrgDO::getDescription, reqVO.getDescription())
                .eqIfPresent(OrgDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OrgDO::getSequence, reqVO.getSequence())
                .eqIfPresent(OrgDO::getVersion, reqVO.getVersion())
                .eqIfPresent(OrgDO::getOrgCode, reqVO.getOrgCode())
                .eqIfPresent(OrgDO::getDeptCode, reqVO.getDeptCode())
                .eqIfPresent(OrgDO::getPositionCode, reqVO.getPositionCode())
                .eqIfPresent(OrgDO::getPersonMemberCode, reqVO.getPersonMemberCode())
                .eqIfPresent(OrgDO::getIsCenter, reqVO.getIsCenter())
                .eqIfPresent(OrgDO::getCenterCode, reqVO.getCenterCode())
                .eqIfPresent(OrgDO::getOrgId, reqVO.getOrgId())
                .eqIfPresent(OrgDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(OrgDO::getPositionId, reqVO.getPositionId())
                .eqIfPresent(OrgDO::getPersonMemberId, reqVO.getPersonMemberId())
                .eqIfPresent(OrgDO::getCenterId, reqVO.getCenterId())
                .likeIfPresent(OrgDO::getOrgName, reqVO.getOrgName())
                .likeIfPresent(OrgDO::getDeptName, reqVO.getDeptName())
                .likeIfPresent(OrgDO::getPositionName, reqVO.getPositionName())
                .likeIfPresent(OrgDO::getPersonMemberName, reqVO.getPersonMemberName())
                .likeIfPresent(OrgDO::getCenterName, reqVO.getCenterName())
                .likeIfPresent(OrgDO::getFullSequence, reqVO.getFullSequence())
                .likeIfPresent(OrgDO::getFullOrgKindId, reqVO.getFullOrgKindId())
                .eqIfPresent(OrgDO::getIsVirtual, reqVO.getIsVirtual())
                .eqIfPresent(OrgDO::getHrCode, reqVO.getHrCode())
                .eqIfPresent(OrgDO::getHrId, reqVO.getHrId());

        String orgKindIds = reqVO.getOrgKindIds();
        if (!StrUtil.isBlank(orgKindIds)) {
            String[] orgKindId = orgKindIds.split(",");
            wrapperX.and(wp -> {
                LambdaQueryWrapper<OrgDO> orgWp = wp;
                Arrays.stream(orgKindId).forEach(kindId -> {
                    orgWp.or();
                    orgWp.eq(OrgDO::getOrgKindId, kindId);
                });
            });
        }

        wrapperX.orderByAsc(OrgDO::getSequence);
        return selectPage(reqVO, wrapperX);
    }

    default List<OrgDO> selectList(OrgExportReqVO reqVO) {
        LambdaQueryWrapperX<OrgDO> wrapperX = new LambdaQueryWrapperX<OrgDO>()
                .eqIfPresent(OrgDO::getTypeId, reqVO.getTypeId())
                .eqIfPresent(OrgDO::getCode, reqVO.getCode())
                .likeIfPresent(OrgDO::getName, reqVO.getName())
                .likeIfPresent(OrgDO::getLongName, reqVO.getLongName())
                .eqIfPresent(OrgDO::getParentId, reqVO.getParentId())
                .likeIfPresent(OrgDO::getFullId, reqVO.getFullId())
                .likeIfPresent(OrgDO::getFullCode, reqVO.getFullCode())
                .likeIfPresent(OrgDO::getFullName, reqVO.getFullName())
                .eqIfPresent(OrgDO::getOrgKindId, reqVO.getOrgKindId())
                .eqIfPresent(OrgDO::getDepth, reqVO.getDepth())
                .eqIfPresent(OrgDO::getPersonId, reqVO.getPersonId())
                .eqIfPresent(OrgDO::getNodeKindId, reqVO.getNodeKindId())
                .likeIfPresent(OrgDO::getDescription, reqVO.getDescription())
                .eqIfPresent(OrgDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OrgDO::getSequence, reqVO.getSequence())
                .eqIfPresent(OrgDO::getVersion, reqVO.getVersion())
                .eqIfPresent(OrgDO::getOrgCode, reqVO.getOrgCode())
                .eqIfPresent(OrgDO::getDeptCode, reqVO.getDeptCode())
                .eqIfPresent(OrgDO::getPositionCode, reqVO.getPositionCode())
                .eqIfPresent(OrgDO::getPersonMemberCode, reqVO.getPersonMemberCode())
                .eqIfPresent(OrgDO::getIsCenter, reqVO.getIsCenter())
                .eqIfPresent(OrgDO::getCenterCode, reqVO.getCenterCode())
                .eqIfPresent(OrgDO::getOrgId, reqVO.getOrgId())
                .eqIfPresent(OrgDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(OrgDO::getPositionId, reqVO.getPositionId())
                .eqIfPresent(OrgDO::getPersonMemberId, reqVO.getPersonMemberId())
                .eqIfPresent(OrgDO::getCenterId, reqVO.getCenterId())
                .likeIfPresent(OrgDO::getOrgName, reqVO.getOrgName())
                .likeIfPresent(OrgDO::getDeptName, reqVO.getDeptName())
                .likeIfPresent(OrgDO::getPositionName, reqVO.getPositionName())
                .likeIfPresent(OrgDO::getPersonMemberName, reqVO.getPersonMemberName())
                .likeIfPresent(OrgDO::getCenterName, reqVO.getCenterName())
                .likeIfPresent(OrgDO::getFullSequence, reqVO.getFullSequence())
                .likeIfPresent(OrgDO::getFullOrgKindId, reqVO.getFullOrgKindId())
                .eqIfPresent(OrgDO::getIsVirtual, reqVO.getIsVirtual())
                .eqIfPresent(OrgDO::getHrCode, reqVO.getHrCode())
                .eqIfPresent(OrgDO::getHrId, reqVO.getHrId());
        String orgKindIds = reqVO.getOrgKindIds();

        if (!StrUtil.isBlank(orgKindIds)) {
            String[] orgKindId = orgKindIds.split(",");
            wrapperX.and(wp -> {
                LambdaQueryWrapper<OrgDO> orgWp = wp;
                Arrays.stream(orgKindId).forEach(kindId -> {
                    orgWp.or();
                    orgWp.eq(OrgDO::getOrgKindId, kindId);
                });
            });
        }

        wrapperX.orderByAsc(OrgDO::getSequence);
        return selectList(wrapperX);

    }


    default List<OrgDO> selectAllList(OrgPageReqVO reqVO) {
        LambdaQueryWrapperX<OrgDO> wrapperX = new LambdaQueryWrapperX<OrgDO>()
                .eqIfPresent(OrgDO::getTypeId, reqVO.getTypeId())
                .eqIfPresent(OrgDO::getCode, reqVO.getCode())
                .likeIfPresent(OrgDO::getName, reqVO.getName())
                .likeIfPresent(OrgDO::getLongName, reqVO.getLongName())
                .eqIfPresent(OrgDO::getParentId, reqVO.getParentId())
                .likeIfPresent(OrgDO::getFullId, reqVO.getFullId())
                .likeIfPresent(OrgDO::getFullCode, reqVO.getFullCode())
                .likeIfPresent(OrgDO::getFullName, reqVO.getFullName())
                .eqIfPresent(OrgDO::getOrgKindId, reqVO.getOrgKindId())
                .eqIfPresent(OrgDO::getDepth, reqVO.getDepth())
                .eqIfPresent(OrgDO::getPersonId, reqVO.getPersonId())
                .eqIfPresent(OrgDO::getNodeKindId, reqVO.getNodeKindId())
                .likeIfPresent(OrgDO::getDescription, reqVO.getDescription())
                .eqIfPresent(OrgDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OrgDO::getSequence, reqVO.getSequence())
                .eqIfPresent(OrgDO::getVersion, reqVO.getVersion())
                .eqIfPresent(OrgDO::getOrgCode, reqVO.getOrgCode())
                .eqIfPresent(OrgDO::getDeptCode, reqVO.getDeptCode())
                .eqIfPresent(OrgDO::getPositionCode, reqVO.getPositionCode())
                .eqIfPresent(OrgDO::getPersonMemberCode, reqVO.getPersonMemberCode())
                .eqIfPresent(OrgDO::getIsCenter, reqVO.getIsCenter())
                .eqIfPresent(OrgDO::getCenterCode, reqVO.getCenterCode())
                .eqIfPresent(OrgDO::getOrgId, reqVO.getOrgId())
                .eqIfPresent(OrgDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(OrgDO::getPositionId, reqVO.getPositionId())
                .eqIfPresent(OrgDO::getPersonMemberId, reqVO.getPersonMemberId())
                .eqIfPresent(OrgDO::getCenterId, reqVO.getCenterId())
                .likeIfPresent(OrgDO::getOrgName, reqVO.getOrgName())
                .likeIfPresent(OrgDO::getDeptName, reqVO.getDeptName())
                .likeIfPresent(OrgDO::getPositionName, reqVO.getPositionName())
                .likeIfPresent(OrgDO::getPersonMemberName, reqVO.getPersonMemberName())
                .likeIfPresent(OrgDO::getCenterName, reqVO.getCenterName())
                .likeIfPresent(OrgDO::getFullSequence, reqVO.getFullSequence())
                .likeIfPresent(OrgDO::getFullOrgKindId, reqVO.getFullOrgKindId())
                .eqIfPresent(OrgDO::getIsVirtual, reqVO.getIsVirtual())
                .eqIfPresent(OrgDO::getHrCode, reqVO.getHrCode())
                .eqIfPresent(OrgDO::getHrId, reqVO.getHrId());
        String orgKindIds = reqVO.getOrgKindIds();

        if (!StrUtil.isBlank(orgKindIds)) {
            String[] orgKindId = orgKindIds.split(",");
            wrapperX.and(wp -> {
                LambdaQueryWrapper<OrgDO> orgWp = wp;
                Arrays.stream(orgKindId).forEach(kindId -> {
                    orgWp.or();
                    orgWp.eq(OrgDO::getOrgKindId, kindId);
                });
            });
        }

        wrapperX.orderByAsc(OrgDO::getSequence);
        return selectList(wrapperX);
    }

    List<OrgDO> selectPsmList(Long systemUserId);


    void deletePsmById(String id);

    List<OrgDO> getOrgListByUserId(Long userId);

    List<OrgDO> selectPsmListByFullIds(@Param("fullIds") List<String> personFullIds);
}
